function WechatOA() {
	
	var _this = this;
	var app;
	var $$;
	var mainView;
	var ctxPath;
	var prefix;
	var wxHelper;
	var loading = false;
	
	this.showPreloader = function () {
		if($$(".modal-overlay.modal-overlay-visible").length == 0){
			app.showPreloader();
		}
	};
	
	this.hidePreloader = function () {
		app.hidePreloader();
	};
	
	this.init = function(baseUrl,prfx,user){
		ctxPath = baseUrl;
		prefix = prfx;
		
		//Add view
		mainView = app.addView('.view-main', {
			//dynamicNavbar: true
		});
		
		$$("#menu-link .item-link").on('click', function (e) {
			app.closePanel();
		});
		
		wxHelper = new WechatHelper(ctxPath,prefix,app);
		this.initTemplate7(user);
		this.initCommonOption();
		this.initPages();
	}
	this._init = function(){
		
		app = new Framework7({
			cache : false,
			pushState: true,
			pushStateSeparator:"#!",
			/*pushStateRoot : "",*/
			fastClicks : true,
			modalTitle : "Wechat OA",
			modalButtonOk : "确定",
			modalButtonCancel : "取消",
			/*modalPreloaderTitle : "",*/
			onAjaxStart: function (xhr) {
				_this.showPreloader();
			},
			onAjaxComplete: function (xhr) {
				_this.hidePreloader();
			}
		});

		// Export selectors engine
		$$ = Dom7;
		
	};
	
	this.loginScreen = function(){
		app.loginScreen();
	};
	
	this.initTemplate7 = function(user){
		Template7.global = {};
		Template7.global.loginUser = user;
		Template7.registerHelper("formatDate", formatDate);
	};
	
	this.isSuccess = function(resData, jwres){
		if(jwres.statusCode == 201 || (jwres.statusCode == 200 && resData.success)){
			return true;
		}
		return false;
	}
	
	this.initCommonOption = function (){
		
		$$(document).on('pageInit', function (e) {
			var page = e.detail.page;
			$$(".sails-btn-submit", page.container).on('click', function(){
				var form = $(this).closest('form');
				_this.configValidator(form);
				//Validate
				if(!form.valid()){
					return;
				}
				_this.showPreloader();
				var json = form.serializeObject();
				var apiPath = $(this).attr('api-path');
				var cbPath = $(this).attr('cb-path');
				var beforeSubmit = form.attr('before-submit');
				
				var callback = function(json){
					io.socket.post(ctxPath + apiPath, json, function(resData, jwres){
						_this.hidePreloader();
						if(_this.isSuccess(resData, jwres)){
							if(cbPath){
								mainView.router.loadPage(ctxPath + prefix + cbPath);
							}else{
								mainView.router.refreshPage();
							}
						}else{
							app.alert(resData.message);
						}
					});
				}
				
				if(beforeSubmit){
					var beforeSubmitFun = eval('_this.' + beforeSubmit);
					beforeSubmitFun($(this), json, callback);
				}else{
					callback(json);
				}
				
			});
			
			$$(".sails-btn-delete", page.container).on('click', function(){
				
				var pkid = $(this).attr('pkid');
				var path = $(this).attr('api-path');
				var message = $(this).attr('message');
				
				app.confirm(message, function () {
					_this.showPreloader();
					io.socket.delete(ctxPath + path, {'id':pkid}, function(resData, jwres){
						_this.hidePreloader();
						if(jwres.statusCode == 200){
							mainView.router.refreshPage();
						}else{
							app.alert(resData.message);
						}
					});
				});
			});
			
		});
	};
	
	this.configValidator = function(form){
		var rules = {};
		form.find("*[name]").each(function(){
			var validate = $(this).attr("validate");
			if(!isBlank(validate)){
				rules[$(this).attr('name')] = eval("(" + validate +  ")");
			}
		});
		
		form.validate({
			rules : rules,
			showErrors : function(errorMap, errorList) {
				if(errorList.length == 0)
					return;
				var popoverHTML = 
					'<div class="popover" style="min-width:160px;width:auto;"><div class="popover-inner">'+
					'<div class="content-block" style="margin:15px 0">'+ errorList[0].message + '</div>'+
					'</div></div>';
				
				app.popover(popoverHTML, errorList[0].element);
			}
		});
	};
	
	this.initPages = function(){
		
		app.onPageInit('note-detail form-detail', function (page) {
			
			$$(".form-form-check .btn-form-check").on('click', function(e){
				var chkFlag = $(this).attr('check-flag');
				var form = $(this).closest('form');
				var json = form.serializeObject();
				json.form.state = chkFlag;
				json.opinion.action = chkFlag;
				
				if(json.opinion.opinion.replace(/\s+/g,'') == ""){
					if(chkFlag == "1"){
						json.opinion.opinion = "驳回";
					}else if(chkFlag == "2"){
						json.opinion.opinion = "同意";
					}else if(chkFlag == "3"){
						json.opinion.opinion = "完成";
					}
				}
				
				io.socket.post(ctxPath + prefix + '/form/check', json, function(resData, jwres){
					_this.hidePreloader();
					if(_this.isSuccess(resData, jwres)){
						mainView.router.refreshPage();
					}else{
						app.alert(resData.message);
					}
				});
			});
			
		});
		
		app.onPageInit('contacts-list', function (page) {
			if(page.query.option == 'true'){
				$$("#div-contacts-list .item-link").attr("href","");
				$$("#div-contacts-list .item-link").on('click', function(e){
					var userid = $(this).attr("userid");
					app.confirm("真的要发送给 [ "+$(this).find(".item-title").text()+" ] 吗？", function () {
						_this.showPreloader();
						var pkid = page.query.pkid;
						var apiPath = prefix + '/form/send';
						var cbPath = '/form/list'
						var data = {
							'user' : userid,
							'pkid' : pkid
						}
						if(page.query.type == 'note'){
							apiPath = prefix + '/note/send';
							cbPath = '/note/list'
						}
						io.socket.post(ctxPath + apiPath, data, function(resData, jwres){
							_this.hidePreloader();
							if(_this.isSuccess(resData, jwres)){
								mainView.router.loadPage(ctxPath + prefix + cbPath);
							}else{
								app.alert(resData.message);
							}
						});
					});
				});
			}
		});
		
		
		app.onPageInit('form-create post-create', function (page) {
			
			$$(".wx-image-selector a").on('click', function(e){
				wxHelper.chooseImages($(".wx-image-selector"));
			});
			
		});
		
		app.onPageInit('form-detail contacts-detail post-list', function (page) {
			wxHelper.previewImage();
		});
		
		app.onPageInit('post-list', function (page) {
			$$(".btn-comment-create").on('click', function(e){
				var postid = $(this).attr('post-id');
				app.prompt('请填写评论内容', function (value) {
					io.socket.post('/api/comment', {
						'content' : value,
						'owner' : Template7.global.loginUser.userid,
						'post' : postid
					}, function(resData, jwres){
						if(_this.isSuccess(resData, jwres)){
							mainView.router.refreshPage();
						}else{
							app.alert(resData.message);
						}
					});
				});
			});
			
			$$(".btn-add-likes").on('click', function(e){
				var postid = $(this).attr('post-id');
				var apiPath = prefix + '/post/likes?pkid=' + postid;
				io.socket.get(apiPath, function(resData, jwres){
					if(_this.isSuccess(resData, jwres)){
						mainView.router.refreshPage();
					}else{
						app.alert(resData.message);
					}
				});
			});
		});
		
		app.onPageInit('form-list note-list form-check', function (page) {
			
			var searchUrl = '';
			
			if(page.name == "note-list"){
				searchUrl = ctxPath + prefix + '/note/list';
			}else if(page.name == "form-list"){
				searchUrl = ctxPath + prefix + '/form/list';
			}else if(page.name == "form-check"){
				searchUrl = ctxPath + prefix + '/form/check';
			}
			
			$$(".subnavbar .button.prev").on('click', function(e){
				var current = $(this).attr('current');
				mainView.router.load({url:searchUrl+'?action=prev&current='+current, pushState:false, animatePages:false, reload:true});
			});
			$$(".subnavbar .button.next").on('click', function(e){
				var current = $(this).attr('current');
				mainView.router.load({url:searchUrl+'?action=next&current='+current, pushState:false, animatePages:false, reload:true});
			});
		});
		
		app.onPageInit('signin', function (page) {
			_this.initCalendar("#calendar-inline-container");
		});
	};

	
	this.uploadFormImages = function(link, json, cb){
		var form = link.closest('form');
		var files = [];
		wxHelper.uploadImages(function(images){
			if(images){
				for (var i = 0; i < images.length; i++) {
					var file = {
						'mediaid' : images[i]
					};
					files.push(file);
				}
				json.attachments = files;
			}
			cb(json);
		});
	};
	
	this.vacationCheck = function(link, json, cb){
		var apiPath = prefix + '/vacation/check';
		io.socket.get(ctxPath + apiPath, {"hours":json.hours, "type":json.type}, function(resData, jwres){
			_this.hidePreloader();
			if(_this.isSuccess(resData, jwres)){
				if(resData.result == true){
					cb(json);
				}else{
					app.alert('剩余年假不足！');
				}
			}else{
				app.alert(resData.message);
			}
		});
	};
	
	this.initCalendar = function(selector){
		var monthNames = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月' , '九月' , '十月', '十一月', '十二月'];
		 
		var calendarInline = app.calendar({
		    container: selector,
		    value: [new Date()],
		    weekHeader: false,
		    toolbarTemplate: 
		        '<div class="toolbar calendar-custom-toolbar">' +
		            '<div class="toolbar-inner">' +
		                '<div class="left">' +
		                    '<a href="#" class="link icon-only"><i class="ti-angle-left"></i></a>' +
		                '</div>' +
		                '<div class="center"></div>' +
		                '<div class="right">' +
		                    '<a href="#" class="link icon-only"><i class="ti-angle-right"></i></a>' +
		                '</div>' +
		            '</div>' +
		        '</div>',
		        
		    onOpen: function (p) {
		        $$('.calendar-custom-toolbar .center').text(monthNames[p.currentMonth] +', ' + p.currentYear);
		        $$('.calendar-custom-toolbar .left .link').on('click', function () {
		            calendarInline.prevMonth();
		        });
		        $$('.calendar-custom-toolbar .right .link').on('click', function () {
		            calendarInline.nextMonth();
		        });
		    },
		    onMonthYearChangeStart: function (p) {
		        $$('.calendar-custom-toolbar .center').text(monthNames[p.currentMonth] +', ' + p.currentYear);
		    }
		});
	};
	
	this._init();
}
